package com.huatai.bi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bi.entity.DatasourceEntity;
import com.huatai.bi.entity.TableColumnEntity;
import com.huatai.bi.entity.TableEntity;
import com.huatai.bi.mapper.TableColumnMapper;
import com.huatai.bi.query.TableColumnQuery;
import com.huatai.bi.service.DatasourceService;
import com.huatai.bi.service.TableColumnService;
import com.huatai.bi.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TableColumnServiceImpl extends ServiceImpl<TableColumnMapper, TableColumnEntity> implements TableColumnService {

    @Autowired
    DatasourceService datasourceService;
    @Lazy
    @Autowired
    TableService tableService;

    @Override
    public List<TableColumnEntity> list(TableColumnQuery query){
        LambdaQueryWrapper<TableColumnEntity> qw = Wrappers.lambdaQuery();
        qw.eq(null != query.getDatasourceId(),TableColumnEntity::getDatasourceId,query.getDatasourceId());
        qw.eq(null != query.getTableId(),TableColumnEntity::getTableId,query.getTableId());
        qw.orderBy(true,true,TableColumnEntity::getDatasourceId,TableColumnEntity::getTableId,TableColumnEntity::getColumnPosition);
        List<TableColumnEntity> list = super.list(qw);
        buildExInfo(list);
        return list;
    }

    public Page page(TableColumnQuery query){
        int pageIndex = 1;
        int pageSize = 10;
        if(null != query.getPageIndex() && null != query.getPageSize()){
            pageIndex = query.getPageIndex() > 0 ? query.getPageIndex() : 1;
            pageSize = query.getPageSize() > 0 ? query.getPageSize() : 10;
        }
        LambdaQueryWrapper<TableColumnEntity> qw = Wrappers.lambdaQuery();
        qw.eq(null != query.getDatasourceId(),TableColumnEntity::getDatasourceId,query.getDatasourceId());
        qw.eq(null != query.getTableId(),TableColumnEntity::getTableId,query.getTableId());
        qw.orderBy(true,true,TableColumnEntity::getDatasourceId,TableColumnEntity::getTableId,TableColumnEntity::getColumnPosition);
        Page page = page(new Page<TableColumnEntity>(pageIndex,pageSize),qw);
        buildExInfo(page.getRecords());
        return page;
    }

    @Override
    public void buildExInfo(TableColumnEntity data) {
        if(null == data){
            return;
        }
        DatasourceEntity datasource = datasourceService.getById(data.getDatasourceId());
        TableEntity table = tableService.getById(data.getTableId());
        if(null != datasource){
            data.setDatasourceName(datasource.getName());
        }
        if(null != table){
            data.setTableName(table.getTableName());
        }
    }
    @Override
    public void buildExInfo(List<TableColumnEntity> list) {
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        List<DatasourceEntity> datasourceList = datasourceService.list();
        Map<Long,String> datasourceNameMap = datasourceList.stream().collect(Collectors.toMap(DatasourceEntity::getId,DatasourceEntity::getName));

        List<TableEntity> tableList = tableService.list();
        Map<Long,String> tableNameMap = tableList.stream().collect(Collectors.toMap(TableEntity::getId,TableEntity::getTableName));

        list.stream().forEach(item ->{
            item.setTableName(tableNameMap.get(item.getTableId()));
            item.setDatasourceName(datasourceNameMap.get(item.getDatasourceId()));
        });

    }

}
